home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Languages / Masm V6.11 / SAMPLES / DOSDEV / ATOMTEST.AS$ / ATOMTEST
Encoding:
Text File  |  1992-11-12  |  7.1 KB  |  224 lines

  1. ;----------------------------------------------------------------------------;
  2. ; ATOMTEST.ASM: A minimal tester for the atoms device driver             ;
  3. ;----------------------------------------------------------------------------;
  4. ; A simple test of ATMS, with minimal error testing. It reads a string from  ;
  5. ; the keyboard, writes it to the device, and immediately reads from the      ;
  6. ; device. It also uses itoh to give the length of the result string returned ;
  7. ; by the driver. Quit by inputting nothing or by CTRL+C                 ;
  8. ;                                         ;
  9. ; To communicate with the ATMS driver, we first open it as if it were         ;
  10. ; a file, using @Openfile. The system will return a handle, with which,         ;
  11. ; we can begin communicating to the driver.                                  ;
  12. ;                                                                            ;
  13. ; By passing a null terminated string to @Write, you can do the following    ;
  14. ;         if string =  '<variable>', search for <variable>                   ;
  15. ;         if string =  '<variable>=' delete <variable>                       ;
  16. ;         if string =  '<variable>=value' insert <variable>                  ;
  17. ; This allows us to 'browse' the data stored by ATMS.                        ;
  18. ;                                                                            ;
  19. ; Calling @Read will return the current <variable> text.                     ;
  20. ;----------------------------------------------------------------------------;
  21.  
  22.     include    dos.inc            ; contains all the @ macros
  23.  
  24.            .model  small, c, os_dos
  25.            .386
  26.     .dosseg
  27.         .stack
  28.     .data
  29.  
  30. dname   BYTE    'ATMS',0
  31. handle  WORD    0
  32. sMenu    BYTE    cr,lf,'     ATMS Sample Device Driver',cr,lf,
  33.         cr,lf,'     Select Device Function',cr,lf,
  34.             cr,lf,'     1. Write to Device Only',
  35.         cr,lf,'     2. Read from Device Only',
  36.         cr,lf,'     3. Inquire Device Output Status',
  37.         cr,lf,'     0. Quit',
  38.         cr,lf,'     Other Keys: Write & Read',
  39.         cr,lf,'                 Until Return',cr,lf,eom
  40. sPrompt BYTE    cr,lf,'     Please Press a Key: ',eom        
  41. sInput  BYTE     cr,lf, 'Input? ', eom
  42. sError  BYTE    cr,lf,lf,'Error: Could not open the driver',cr,lf,eom
  43. sQuit    BYTE    cr,lf,lf,'Quit',cr,lf,eom
  44. outBuf    BYTE    140 dup (?)
  45. output  BYTE    cr,lf
  46. value     BYTE    '0000: '
  47. inBuf   BYTE    140 dup (?)
  48. inLen    WORD    SIZEOF inBuf
  49. outLen  WORD    SIZEOF outBuf
  50. cr      EQU     0Dh                 
  51. lf      EQU     0Ah                 
  52. eom     EQU     '$'                 
  53. ; Table of hexadecimal digits used for the hex translation
  54. hex    BYTE    '0123456789ABCDEF'
  55.  
  56.     .code
  57.  
  58. int2hex PROTO, :WORD, :WORD
  59.  
  60.         .startup
  61. START: 
  62.     @Openfile dname, 42h        ; open the driver named 'ATMS' for
  63.                     ; read/write, no_deny access
  64.     jc error            ; carry will be set if error
  65.     mov    handle, ax        ; o.w. the handle is in ax
  66.  
  67.     mov    ax, 4401h        ; function 4401h: set device data
  68.     mov    bx, handle        ; handle of device from Openfile
  69.     mov    dx, 00a0h        ; status: it's a device (bit 7)
  70.     int    21h            ; and it's in raw/binary mode (bit 5)
  71.                     ; and do the interrupt
  72.  
  73. Menu_Loop:
  74.  
  75.     @ShowStr sMenu            ; show the menu
  76.     
  77.     .WHILE 1    
  78.  
  79.     @ShowStr sPrompt        ; show the prompt
  80.     @GetChar            ; get character into ax
  81.  
  82.     push    ax            ; save ax for later
  83.     @ShowChar cr,lf            ; for formatting
  84.     pop    ax            ; now is the time
  85.     
  86.     .IF (al == '0')            ; if the user wants to quit
  87.  
  88.     jmp quit            ; then quit
  89.  
  90.     .ELSEIF (al == '1')        ; 1 would be write to device:
  91.                     ; code is somewhat repeated since other
  92.                     ; (in lower loop) is different
  93.                     
  94.     @ShowStr sInput            ; show a friendly input? message
  95.             
  96.     @Read     outBuf, outLen        ; read from keyboard (no handle)
  97.  
  98.     .BREAK     .IF (ax==2)        ; read from keyboard appends a '$'
  99.                     ; after the cr. if we have only cr'$'
  100.                     ; then end the loop
  101.     
  102.     mov    bx, ax            ; we'll need to use a base pointer 
  103.     mov    outBuf[bx-2],0        ; to set the 'cr' to a 0 (null) so that
  104.                     ; the driver can handle the string
  105.  
  106.     @Write    outBuf, outLen, handle  ; write the string to the device, the
  107.                     ; actual count is not necessary
  108.  
  109.     .ELSEIF (al == '2')        ; 2: read only
  110.  
  111.     call do_read            ; do the read
  112.  
  113.     .ELSEIF (al == '3')
  114.  
  115.     mov    bx, handle        ; get handle into BX
  116.     mov    ax, 4407h        ; function 4407h: Check Dev.Output Stat
  117.     int    21h            ; do the interrupt
  118.     .IF CARRY?            ; carry set means error
  119.     @ShowStr sQuit            ; here you could handle errors
  120.     .ENDIF    
  121.  
  122.     .ELSE
  123.  
  124.     jmp     Input_Loop
  125.     
  126.  
  127.     .ENDIF                ; continue the other loop
  128.  
  129.     .ENDW
  130.     
  131. ;---- loop that reads from keyboard, writes to device, and reads from device
  132. ;     until nothing inputed
  133.  
  134. Input_Loop:
  135.     .WHILE 1            ; loop for input
  136.  
  137.     @ShowStr sInput            ; show a friendly input? message
  138.             
  139.     @Read     outBuf, outLen        ; read from keyboard (no handle)
  140.  
  141.     .IF (ax==2)            ; read from keyboard appends a '$'
  142.     jmp Menu_Loop            ; after the cr. if we have only cr'$'
  143.     .ENDIF                ; then end the loop
  144.     
  145.     mov    bx, ax            ; we'll need to use a base pointer 
  146.     mov    outBuf[bx-2],0        ; to set the 'cr' to a 0 (null) so that
  147.                     ; the driver can handle the string
  148.  
  149.     @Write    outBuf, outLen, handle  ; write the string to the device, the
  150.                     ; actual count is not necessary
  151.  
  152.     call     do_read            ; do a read
  153.  
  154.     .ENDW                ; cycle the loop
  155.     
  156. quit:    @ShowStr sQuit          ; quit: print the quit message
  157.     .exit                ; and exit
  158.  
  159. error:  @ShowStr sError            ; error: print the error message
  160.     jmp    quit            ; and go to quit
  161.     
  162.  
  163. ;----------------------------------------------------------------------------;
  164. ; do_read: reads from device, calls int2hex, prints output. since it's used  ;
  165. ; in two places, it's a routine                             ;
  166. ;----------------------------------------------------------------------------;
  167.  
  168. do_read PROC
  169.  
  170.     @Read    inBuf, inLen, handle    ; read from the device into the inBuf
  171.  
  172.     mov    bx,ax            ; again need to use a base pointer
  173.     mov    inBuf[bx-1], cr        ; set the '\0' to a cr
  174.     mov    inBuf[bx],lf        ; set a lf after that
  175.     mov    inBuf[bx+1],eom        ; and the end-of-message after that
  176.  
  177.     INVOKE    int2hex,ax,ADDR value   ; int2hex will write to the '0000'
  178.  
  179.     @ShowStr output            ; show the value (followed by inBuf)
  180.  
  181.     ret
  182.     
  183. do_read ENDP
  184.  
  185. ;----------------------------------------------------------------------------;
  186. ; int2hex                                      ;
  187. ; Converts a WORD into its hexadecimal representation.                  ;
  188. ; Based on Chapter 4 of the MASM Programmer's guide                 ;
  189. ;----------------------------------------------------------------------------;
  190.  
  191. int2hex    PROC USES ax bx si, number:WORD, string:WORD
  192.  
  193.     mov    bx, OFFSET hex        ; load table address
  194.     mov    si, string
  195.  
  196.     mov    ax, number        ; load value to convert    
  197.     and    ax, 0F000h        ; mask out all but the first byte
  198.     shr    ax, 12            ; shift
  199.     xlat                ; translate
  200.     mov    [si], al        ; store
  201.  
  202.     mov    ax, number        ; load value to convert    
  203.     and    ax, 00F00h        ; mask out all but the second byte
  204.     shr    ax, 8            ; shift
  205.     xlat                ; translate
  206.     mov    1[si], al        ; store
  207.  
  208.     mov    ax, number        ; load value to convert    
  209.     and    ax, 000F0h        ; mask out all but the third byte
  210.     shr    ax, 4            ; shift right to get into table index
  211.     xlat                ; translate
  212.     mov    2[si], al        ; store as second to last byte
  213.  
  214.     mov    ax, number        ; load value to convert    
  215.     and    ax, 0000Fh        ; mask out all but the last byte
  216.     xlat                ; translate
  217.     mov    3[si], al        ; store as last byte in string
  218.  
  219.     ret
  220.  
  221. int2hex    ENDP
  222.  
  223.     END    
  224.